/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GameMiniMap.java
 *
 * Created on 2011-05-23, 06:13:37
 */

package jtmcraft.game;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import jtmcraft.game.units.GameUnit;

/**
 *
 * @author tom
 */
public class GameMiniMap extends javax.swing.JPanel {

    private BufferedImage backbuffor;
    private BufferedImage emptybuffor;
    private Graphics2D bbGd2;
    private Graphics2D ebGd2;

    private Color backgroundColor;

    private int mapWidth = 0;
    private int mapHeight = 0;
    private float factorX = 1;
    private float factorY = 1;

    private boolean init = false;

    /** Creates new form GameMiniMap */
    public GameMiniMap() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                formMousePressed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void formMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMousePressed
        // TODO add your handling code here:
    }//GEN-LAST:event_formMousePressed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables

    public void Init( int mapWidth, int mapHeight )
    {
        this.mapHeight = mapHeight;
        this.mapWidth = mapWidth;

        factorX = (float) getWidth() / (float) mapWidth;
        factorY = (float) getHeight() / (float) mapHeight;

        backbuffor =  getGraphicsConfiguration().createCompatibleImage(getWidth(), getHeight());
        emptybuffor = getGraphicsConfiguration().createCompatibleImage(getWidth(), getHeight());
        bbGd2 = backbuffor.createGraphics();
        ebGd2 = emptybuffor.createGraphics();
        backgroundColor = new Color( 0, 0, 0 );
        bbGd2.setColor(backgroundColor);
        bbGd2.fillRect(0, 0, getWidth(), getHeight());
        ebGd2.setColor(backgroundColor);
        ebGd2.fillRect(0, 0, getWidth(), getHeight());
        init = true;
    }

    public void drawUnit(GameUnit gameUnit, Color color) {
        if (init) {
            bbGd2.setColor(color);

            int x = (int) (gameUnit.getX() * factorX);
            int y = (int) (gameUnit.getY() * factorY);

            int width = (int) (gameUnit.getWidth() * factorX);
            int height = (int) (gameUnit.getHeight() * factorY);

            bbGd2.fillRect(x, y, width, height);
        }
    }

    public void update()
    {
        if( init )
        {
            Graphics2D gd2 = (Graphics2D) getGraphics();
            gd2.drawImage(backbuffor, 0, 0, null);
        }
    }

    public void clearBackBuffer()
    {
        if( init )
        {
           bbGd2.drawImage(emptybuffor, 0, 0, null);
        }
    }

}
